package com.dq.controller.op.opboss.finance;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.RewardQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.org.Org;
import com.dq.domain.reward.Reward;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.service.AllService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/23 13:53
 */
@RestController
@RequestMapping("/sys/boss/BossReward")
@Api(description = "随机/专项奖励管理")
public class BossRewardController extends AllService {


    @PostMapping("/reward/list")
    @ApiOperation("专项/随机奖励列表")
    public Vo rewardList(@RequestBody RewardQuery rewardQuery)throws AdminException{
        if(null==rewardQuery.getPage() || null==rewardQuery){throw new AdminException("缺少必要的请求参数");}
        Org org = null;
        if(rewardQuery.getOrgId() != null){
            org = dao.get(Org.class,rewardQuery.getOrgId());
        }
        List<Reward> rewardList = rewardService.findByWhere(rewardQuery,org);
        Long totalCount = rewardService.countByWhere(rewardQuery,org);
        PageInfo pageInfo = new PageInfo((List<Reward.Vo>) VO.toVOCollection(rewardList));
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);
    }
    @ApiOperation("添加")
    @PostMapping("add/{orgId}/{staffUserId}")
    public Vo add(@PathVariable Long orgId,@PathVariable Long staffUserId,@RequestBody Reward reward)throws AdminException{

        return rewardService.add(orgId,staffUserId,reward);
    }

    @ApiOperation("编辑")
    @PostMapping("edit/{orgId}/{staffUserId}")
    public Vo edit(@PathVariable Long orgId,@PathVariable Long staffUserId,@RequestBody Reward rewardEdit)throws AdminException{
        return rewardService.edit(orgId,staffUserId,rewardEdit);
    }

    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    public Vo delete(@PathVariable Long id)throws AdminException{
        return rewardService.delete(id);
    }

    @ApiOperation("根据用户工号查")
    @GetMapping("getInfoByCardNo/{cardNo}")
    public Vo getInfoByCardNo(@PathVariable @ApiParam("工号")String cardNo) throws AdminException{
        List<StaffUser> list= staffUserRepository.findByCardNoAndDeletedFalse(cardNo);
        List<StaffUser.SimpleWxUser> wxUserList = new ArrayList<>();
        if(list.size() == 0){
            throw new AdminException("无此用户");
        }
        list.forEach(staffUser -> {
            wxUserList.add(staffUser.toSimpleWxUser());
        });
        return new Vo(wxUserList);
    }
}
